package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.SysPostDo;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.service.ISysPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 岗位信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@RequiredArgsConstructor
@Service
public class SysPostServiceImpl implements ISysPostService {
    private final SysPostMapper sysPostMapper;

    /**
     * 查询岗位信息
     */
    @Override
    public SysPostDo queryById(Long postId) {
        return sysPostMapper.selectById(postId);
    }

    /**
     * 查询岗位信息列表
     */
    @Override
    public TableDataInfo<SysPostDo> queryPageList(SysPostDo entity, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPostDo> lqw = buildQueryWrapper(entity);
        Page<SysPostDo> result = sysPostMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询岗位信息列表
     */
    @Override
    public List<SysPostDo> queryList(SysPostDo entity) {
        LambdaQueryWrapper<SysPostDo> lqw = buildQueryWrapper(entity);
        return sysPostMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysPostDo> buildQueryWrapper(SysPostDo entity) {
        Map<String, Object> params = entity.getParams();
        LambdaQueryWrapper<SysPostDo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(entity.getPostCode()), SysPostDo::getPostCode, entity.getPostCode());
        lqw.like(StringUtils.isNotBlank(entity.getPostName()), SysPostDo::getPostName, entity.getPostName());
        lqw.eq(entity.getPostSort() != null, SysPostDo::getPostSort, entity.getPostSort());
        lqw.eq(StringUtils.isNotBlank(entity.getStatus()), SysPostDo::getStatus, entity.getStatus());
        return lqw;
    }

    /**
     * 新增岗位信息
     */
    @Override
    public Boolean insert(SysPostDo entity) {
        SysPostDo add = BeanUtil.toBean(entity, SysPostDo.class);
        validEntityBeforeSave(add);
        Boolean flag = sysPostMapper.insert(add) > 0;
        if (flag) {
            entity.setPostId(add.getPostId());
        }
        return flag;
    }

    /**
     * 修改岗位信息
     */
    @Override
    public Boolean updateById(SysPostDo entity) {
        SysPostDo update = BeanUtil.toBean(entity, SysPostDo.class);
        validEntityBeforeSave(update);
        return sysPostMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysPostDo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除岗位信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return sysPostMapper.deleteBatchIds(ids) > 0;
    }
}
